<?php
function downloadUrl ($url, $type='GET', $postData='', $headers='',$cookies='',$referer='',$userAgent='http://code.google.com/p/dmyml/') {
    /* Makes an http petition and returns the web content, the headers and the response code number */
    /*Return: Array
                (
                    Html code =>[content] => <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
                                            <TITLE>301 Moved</TITLE></HEAD><BODY>
                                            <H1>301 Moved</H1>
                                            The document has moved
                                            <A HREF="http://www.google.com/">here</A>.
                                            </BODY></HTML>
                                            
                    HTTP response code => [code] => 301
                    HTTP response headers => [headers] => HTTP/1.1 301 Moved Permanently
                                                          Date: Fri, 26 Feb 2010 09:42:22 GMT
                                                          Content-Length: 219
                                                          Content-Type: text/html; charset=UTF-8
                                                          Expires: Sun, 28 Mar 2010 09:42:22 GMT
                                                          Cache-Control: public, max-age=2592000
                                                          Server: gws
                                                          X-XSS-Protection: 0
                                                          Location: http://www.google.com/
                    True if there is a response from the server => [result] => 1
                )
    */
    // create curl resource
    $ch = curl_init();            
    curl_setopt($ch, CURLOPT_URL, $url); //Set the url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,    1); //Return the transfer
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,    1); //As binary
    curl_setopt($ch, CURLOPT_FORBID_REUSE,      1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT,     1);
    curl_setopt($ch, CURLOPT_HEADER,            1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,    0);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,     $type);
    curl_setopt($ch, CURLOPT_USERAGENT,         $userAgent);
    curl_setopt($ch, CURLOPT_NOPROGRESS,        false);
    curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,  'checkExitWhileDownloading');
    curl_setopt($ch, CURLOPT_BUFFERSIZE,        1024);
    if($postData!=''){
        curl_setopt($ch, CURLOPT_POSTFIELDS,        $postData);
    }
    if($cookies!=''){
        curl_setopt($ch, CURLOPT_COOKIE,            $cookies);
    }
    if(is_array($headers)){
        curl_setopt($ch, CURLOPT_HTTPHEADER,    $headers);
    }
    if($referer!=''){
        if(strtoupper($referer)=='AUTO'){ //Set the referer to "protocol://domain.tld/"
            $parseUrl = parse_url($url);
            $referer = $parseUrl['scheme'].'://'.$parseUrl['host'].'/';
        }
        curl_setopt($ch, CURLOPT_REFERER,       $referer);
    }
    
    // $output contains the output string
    $output['content'] = curl_exec($ch);
    
    //Read more information
    $curlInfo = curl_getinfo($ch);
    
    // close curl resource to free up system resources
    curl_close($ch);     
    
    //http response code (200, 404, etc)
    $output['code'] = $curlInfo["http_code"];
    //http response headers
    $output['headers'] = strstr($output['content'],"\r\n\r\n",true);
    //request result
    if($output['content']===false){
        $output['result'] = false;
    }else{
        $output['result'] = true;
    }
    //response body
    $output['content'] = substr($output['content'],strpos($output['content'],"\r\n\r\n")+4);           
    return $output;
}

function downloadUrlRecursive ($url, $type='GET', $postData='', $headers='',$cookies='',$referer='',$userAgent='http://code.google.com/p/dmyml/') {
    $array = downloadUrl($url,$type,$postData,$headers,$cookies,$referer,$userAgent);
    $ReceivedHeaders = split("\r\n",$array['headers']);
    foreach($ReceivedHeaders as $ReceivedHeader){
        if(substr($ReceivedHeader,0,10)=='Location: '){
            return downloadUrlRecursive(substr($ReceivedHeader,10),$type,$postData,$headers,$cookies,$referer,$userAgent);
        }
    }
    return $array;
}

?>